package com.base.mail.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.base.commonlib.http.utils.ScreenUtil;
import com.base.commonlib.utils.DisplayUtil;
import com.base.commonlib.utils.SystemUtil;


/**
 * Created by arvin on 2017/12/4.
 */

public class LetterView extends View {
    private String[] letters;
    private boolean showBkg;
    private int choose;
    private Context mContext;
    private Paint mPaint;
    private LetterSelectedListener mListener;

    public LetterView(Context context) {
        super(context);
    }

    public LetterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        letters = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
        showBkg=false;
        choose=-1;
        mPaint = new Paint();
        mContext=context;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(showBkg)
        {
            canvas.drawColor(Color.parseColor("#40000000"));
        }

        int height=getHeight();
        int width=getWidth();
        float singleHeight=(float) height/(float) letters.length;
        for(int i=0;i<letters.length;i++)
        {
            mPaint.setColor(Color.parseColor("#999999"));
            mPaint.setTypeface(Typeface.DEFAULT);
            mPaint.setAntiAlias(true);
            mPaint.setTextSize(DisplayUtil.dip2px(mContext,10));
            if(i==choose)
            {
                mPaint.setColor(Color.parseColor("#525252"));
                mPaint.setFakeBoldText(true);
            }

            float xPos=(float)(width/2f)-(float) (mPaint.measureText(letters[i])/2f);
            float yPos=(float)(i+1)*singleHeight-(singleHeight/2f-(mPaint.measureText(letters[i])/2f));
            canvas.drawText(letters[i],xPos,yPos,mPaint);
            mPaint.reset();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float y = event.getY();
        int oldChoose=choose;
        int c= (int) ((y/(float) getHeight())*letters.length);
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                if(oldChoose!=c&&mListener!=null&&c>=0&&c<=(letters.length-1))
                {
                    showBkg=true;
                    choose=c;
                    mListener.onLetterSelectedListener(letters[c]);
                    invalidate();
                }

                break;
            case MotionEvent.ACTION_MOVE:
                if(oldChoose!=c&&mListener!=null&&c>=0&&c<=(letters.length-1))
                {
                    showBkg=true;
                    choose=c;
                    mListener.onLetterSelectedListener(letters[c]);
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                showBkg=false;
                choose=-1;
                invalidate();
                break;
            default:
                break;
        }
        return true;
    }

    public void setOnLetterSelectedListener(LetterSelectedListener listener)
    {
        mListener=listener;
    }
    public interface LetterSelectedListener
    {
        void onLetterSelectedListener(String letter);
    }
}
